<?php

use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use kartik\file\FileInput;
use kartik\select2\Select2;
use app\modules\Finance\models\ReceiveAccount;


$data = ReceiveAccount::find()->all();
$arr =  yii\helpers\ArrayHelper::map($data, 'id', 'account');
?>

<div class="finance-form">

    <?php $form = ActiveForm::begin([
                                 'options' => [],
                                 'layout' => 'horizontal',
                                 'fieldConfig' => [
                                    'template' => "{label}\n{beginWrapper}\n{input}\n{hint}\n{error}\n{endWrapper}",
                                    'horizontalCssClasses' => [
                                        'label' => 'col-sm-3',
                                        'wrapper' => 'col-sm-8',
                                    ],
                                 ],
                            ]); ?>

    <?= $form->field($model, 'type')->inline(true)->radioList(Yii::$app->params['finance_type']) ?>

    <?= $form->field($model, 'price_type')->inline(true)->radioList(Yii::$app->params['finance_price_type']) ?>

    <?= $form->field($model, 'receive_account')->dropdownList([]) ?>

    <?= $form->field($model, 'remark')->textInput(['maxlength' => true]) ?>

	<div class="form-group">
		<label class="control-label col-sm-3" for="order-company"><?= Yii::t('app.finance', 'Company') ?></label>
		<div class="col-sm-8">
    	<?= Select2::widget([
    	        'name'  => 'Finance[company]',
    	        'id'  => 'finance-company',
    	        'attribute'  => 'company',
    	        'options' => ['multiple' => false],
    	        'pluginOptions' => array(
    	            'placeholder' => Yii::t('app.finance', 'Company'),
    	            'allowClear' => true,
    	            'tags' => true,
    	            'minimumInputLength' => 1,
    	            'ajax' => [
    	                    'url' => yii\helpers\Url::to(['/finance/finance/suggest']),
    	                    'dataType' => 'json',
    	            ],
    	        ),
				'pluginEvents' => [
    				"change" => "function(event){
    				    var id = event.target.value;
						// 获取联系人其它信息
						if(id){
							$.get('/finance/finance/find-company', { id: id},
								function(data){
									if(data.account){
										$('#finance-account').val(data.account);
									}
									if(data.name){
										$('#finance-name').val(data.name);
									}

 									$('#finance-company').find('option[value='+data.id+']').attr('value', data.company);
							});
						}
    				}",
    			],
    	]) ?>
		</div>
	</div>

    <?= $form->field($model, 'account')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'desc')->textInput(['maxlength' => true]) ?>


    <?= $form->field($model, 'is_invoice')->inline(true)->radioList([1=>'否',2=>'是']) ?>

    <!-- ?= $form->field($model, 'img')->textInput(['maxlength' => true]) ? -->

    <div class="form-group text-center">
        <?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>

    <?php ActiveForm::end(); ?>
</div>


<?php app\components\JsBlock::begin() ?>
<script type="text/javascript">
    $("input[name='Finance[type]']").change(function(){
         var val = $(this).val();
         if(val == 3){
            $(".field-finance-is_invoice").hide();
         }else{
            $(".field-finance-is_invoice").show();
         }
    });




    function add_option(type_id){
        $("#finance-receive_account option").remove();
        $("#finance-remark").val("");
        $.get('/finance/finance/get-account',{type_id: type_id}, function(data){
              data = $.parseJSON(data);  
              var html = "<option>选择收款账号</option>"
              if(data.output){
                 $.each(data.output,function(index,value){
                     html+= "<option value='"+value.id+"' data-remark='"+value.remark+"'>"+value.name+"</option>";
                 });
              }
              $("#finance-receive_account").append(html);               
        });

    }

    add_option(1);

    $("input[name='Finance[price_type]']").change(function(){
         var val = $(this).val();
         add_option(val);
    });

    $("#finance-receive_account").change(function(){
        var remark = $(this).children('option:selected').attr("data-remark");
        $("#finance-remark").val(remark);
    });
   



</script>
<?php app\components\JsBlock::end() ?>

